#include "DataTransfer.h"
#include "AnoPTv8FrameFactory.h"
#include "AnoPTv8.h"
#include "PMU_Task.h"

typedef struct {
    const char name[10];
    const uint8_t frameID;
    const uint16_t usbSendCycleTimeInMs;
    const uint16_t uartSendCycleTimeInMs;
} __attribute__((__packed__))sFrameSendInfo;

const sFrameSendInfo frameSIDef[] = {
    {"power", 0x0D, 20, 20},
    {"batinfo", 0x10, 50, 50},
};
const uint16_t frameCount = sizeof(frameSIDef) / sizeof(sFrameSendInfo);
uint16_t timeBufUsb[frameCount] = {0};
uint16_t timeBufUart[frameCount] = {0};

void DataTransferRunTask(const uint16_t dT_ms)
{
    for (int i = 0; i < frameCount; i++) {
        if (frameSIDef[i].usbSendCycleTimeInMs > 0) {
            timeBufUsb[i] += dT_ms;

            if (timeBufUsb[i] >= frameSIDef[i].usbSendCycleTimeInMs) {
                timeBufUsb[i] -= frameSIDef[i].usbSendCycleTimeInMs;
                DataTransferFrameSend(LT_USBCDC, 0xFF, frameSIDef[i].frameID);
            }
        }

        if (frameSIDef[i].uartSendCycleTimeInMs > 0) {
            timeBufUart[i] += dT_ms;

            if (timeBufUart[i] >= frameSIDef[i].uartSendCycleTimeInMs) {
                timeBufUart[i] -= frameSIDef[i].uartSendCycleTimeInMs;
                DataTransferFrameSend(LT_U3, 0xFF, frameSIDef[i].frameID);
            }
        }
    }
}

void DataTransferFrameSend(const uint8_t linktype, const uint8_t addr, const uint8_t fid)
{
    uint8_t _sbuf[30];
    uint8_t _cnt = 0;

    switch (fid) {
    case 0x0D: {
        uint16_t _tmp = PmuData.batteryVoltage * 100;
        _sbuf[_cnt++] = BYTE0(_tmp);
        _sbuf[_cnt++] = BYTE1(_tmp);
        _tmp = PmuData.batteryCurrent * 100;
        _sbuf[_cnt++] = BYTE0(_tmp);
        _sbuf[_cnt++] = BYTE1(_tmp);
        _tmp = PmuData.nodeVoltage * 100;
        _sbuf[_cnt++] = BYTE0(_tmp);
        _sbuf[_cnt++] = BYTE1(_tmp);
        _tmp = PmuData.nodeCurrent * 100;
        _sbuf[_cnt++] = BYTE0(_tmp);
        _sbuf[_cnt++] = BYTE1(_tmp);
		_tmp = 0;
		_sbuf[_cnt++] = BYTE0(_tmp);
		_sbuf[_cnt++] = BYTE1(_tmp);
        AnoPTv8SendBuf(linktype, addr, fid, _sbuf, _cnt);
        break;
    }

    case 0x10: {
        uint16_t _tmp = PmuData.batteryVoltage * 100;
        _sbuf[_cnt++] = BYTE0(_tmp);
        _sbuf[_cnt++] = BYTE1(_tmp);
        _tmp = PmuData.batteryCellsVotage[0] * 100;
        _sbuf[_cnt++] = BYTE0(_tmp);
        _sbuf[_cnt++] = BYTE1(_tmp);
        _tmp = PmuData.batteryCellsVotage[1] * 100;
        _sbuf[_cnt++] = BYTE0(_tmp);
        _sbuf[_cnt++] = BYTE1(_tmp);
		_tmp = PmuData.batteryCellsVotage[2] * 100;
        _sbuf[_cnt++] = BYTE0(_tmp);
        _sbuf[_cnt++] = BYTE1(_tmp);
        _tmp = PmuData.batteryCellsVotage[3] * 100;
        _sbuf[_cnt++] = BYTE0(_tmp);
        _sbuf[_cnt++] = BYTE1(_tmp);
        _tmp = PmuData.batteryCellsVotage[4] * 100;
        _sbuf[_cnt++] = BYTE0(_tmp);
        _sbuf[_cnt++] = BYTE1(_tmp);
        _tmp = PmuData.batteryCellsVotage[5] * 100;
        _sbuf[_cnt++] = BYTE0(_tmp);
        _sbuf[_cnt++] = BYTE1(_tmp);
        AnoPTv8SendBuf(linktype, addr, fid, _sbuf, _cnt);
        break;
    }
    }
}
